import router from '@/router'
import { MenusData } from '@/types/menus'

const modules = import.meta.glob('@pages/*/*.vue')

const registerRouter = (menus: Array<MenusData>) => {
  menus.forEach(x => {
    if (x.children && x.children.length > 0) {
      registerRouter(<Array<MenusData>>x.children)
      return
    }
    const route = Object.assign({}, x, {
      name: x.id,
      displayName: x.name,
      // FIX: 此处不应该这么写，但是没找到更好的方案，import.meta.glob不行
      component: modules['/src/' + x.component + '.vue']
    })
    router.addRoute('root', route)
  })
}
export {
  registerRouter
}